/**
 WhereIsMyMail Copyright (c) 2012 Artem Kuroptev

 This file is part of WhereIsMyMail. WhereIsMyMail is free software: you can redistribute it and/or modify it
 under the terms of the GNU General Public License as published by the Free Software Foundation,
 either version 3 of the License, or (at your option) any later version.

 WhereIsMyMail is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with WhereIsMyMail. If not, see <http://www.gnu.org/licenses/>.
 */

package com.ucware.wimm.imaps;

import com.ucware.wimm.AbstractUp;

import javax.mail.*;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * $Id: ListFolderTree.java 21 2012-12-05 15:20:43Z kuroptev@gmail.com $
 * @created 01.12.2012
 * @author Artem Kuroptev
 */
public class ListFolderTree
{
    private static final Logger log = Logger.getLogger(ListFolderTree.class.getName());

    protected static void log(Object s) {log.info(s.toString());}
    protected static void log(Throwable s) {log.log(Level.SEVERE, s.getLocalizedMessage(), s);}

    public static void main(String[] args) {
        try {
            AbstractUp.setupLogManager();
            if(args.length < 1) {
                log("com.ucware.wimm.imaps.ListFolderTree $Id: ListFolderTree.java 21 2012-12-05 15:20:43Z kuroptev@gmail.com $");
                log("usage: java com.ucware.wimm.imaps.ListFolderTree <imaps://user:password@host:port>");
                return;
            }

            listFolderTree(args[0], new Properties(System.getProperties()));
        }
        catch (Exception e){
            log(e);
        }
    }

    public static void listFolderTree(final String url, final Properties properties) throws MessagingException
    {
//        properties.put("mail.debug", "true");

        final Session session = Session.getInstance(properties, null);
        final URLName urlName = new URLName(url);
        final Store store = session.getStore(urlName);

        log("Connected: " + store.isConnected());
        log("Connecting to the Store.. ");
//        log("Connecting to the Store: " + urlName); // Should't log this, URL contains password!
        store.connect();
        
        try {
            log("Getting Default folder...");
            final Folder defaultFolder = store.getDefaultFolder();

            log("Listing folders:");
            listSubFilders(defaultFolder);
        }
        finally {
            store.close();
        }
        log("Done at: " + new Date());
    }

    public static void listSubFilders(final Folder folder) throws MessagingException
    {
        log(folder.getFullName());
        log(folder.getURLName());
        for (Folder sub : folder.list()) {
            listSubFilders(sub);
        }
    }
}
